package com.wjq.sale.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjq.sale.entity.Sale_list_goods;
import com.wjq.sale.service.ISale_list_goodsService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.beans.IntrospectionException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjq
 * @since 2020-06-04
 */
@RestController
@RequestMapping("/sale")
@Slf4j
public class Sale_list_goodsController {
    @Autowired
    private ISale_list_goodsService iSale_list_goodsService;

    @RequestMapping("list")
    public IPage<Sale_list_goods> list(@RequestParam(defaultValue = "1") Integer current,@RequestParam(defaultValue = "5") Integer size, Sale_list_goods slg){
        Page page = new Page(current, size);
        return iSale_list_goodsService.selectAll(page,slg);
    }


    @SneakyThrows
    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity<byte[]> downLoadGoodsExcel(Sale_list_goods goodDto) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, ParseException, InvocationTargetException, IOException, IOException {
        log.info("开始打印导出excel 的 日志....");
        XSSFWorkbook xssfSheets = iSale_list_goodsService.downLoadGoodsExcel(goodDto);
        log.info("--------------------------------结果是{}:{}",xssfSheets,1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xssfSheets.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("商品表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }

    @RequestMapping("/updateLoad")
    public void uploadExcel(MultipartFile file) throws Exception {
        iSale_list_goodsService.uploadExcel(file.getInputStream(),file.getOriginalFilename());
    }
}

